package com.share.two.Pub_Sub;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Subscribe1 {


	public static void main(String[] args) throws Exception{
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"sunxj", 
				"123123", 
				"tcp://localhost:61616");
		Connection connection = connectionFactory.createConnection();
		connection.start();
		
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination destination = session.createTopic("topic1");
		MessageConsumer messageConsumer = session.createConsumer(destination);

		while (true) {
			TextMessage msg = (TextMessage)messageConsumer.receive();
			if (msg == null) {
				break;
			}
			System.out.println("订阅者1： " + msg.getText());
		}

		if (connection != null) {
			connection.close();
		}
	}

}
